# coding: utf-8
"""
菜单管理相关接口
"""
import traceback
from flask_restful import Resource, reqparse
from . import menu_api
from ..services.menu_service import MenuService

class MenuTreeApi(Resource):
    """Resource for getting menu tree by record_id."""
    
    def get(self, record_id):
        """Get menu tree by record ID."""
        try:
            # 调用service层处理业务逻辑
            menu_data = MenuService.get_menu_tree_by_record_id(record_id)
            
            # 处理null值
            processed_data = MenuService.process_null_values(menu_data)
            
            # 构造返回结果
            result = {
                'result': 'success',
                'data': processed_data
            }
            
            return result
        except Exception as e:
            print('【获取菜单树状结构错误:】', traceback.format_exc())
            return {'result': 'fail', 'message': 'Server error occurred: {}'.format(str(e))}

class MenuDataApi(Resource):
    """Resource for getting menu data (table and SQL) by menu_id."""
    
    def get(self, menu_id):
        """Get menu data (table and SQL) by menu ID."""
        try:
            # 调用service层处理业务逻辑
            menu_data = MenuService.get_menu_data_by_menu_id(menu_id)
            
            # 构造返回结果
            result = {
                'result': 'success',
                'data': menu_data
            }
            
            return result
        except Exception as e:
            print('【获取菜单数据错误:】', traceback.format_exc())
            return {'result': 'fail', 'message': 'Server error occurred: {}'.format(str(e))}

# 注册路由
menu_api.add_resource(MenuTreeApi, "/<string:record_id>/tree")
menu_api.add_resource(MenuDataApi, "/<int:menu_id>/data")